package com.server.util;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

/**
 * 布隆过滤器
 */
public class BloomFilterUtil<T> {

    /**
     * 预计插入的数据
     * 相同的fpp时，期望数量越大 占用内存越大；相同期望数量，fpp(误差率)越小，占用内存越大
     * 100W     700KB
     * 1000W    7MB
     * 10000W   70MB
     */
    private int expectedInsertions = 1000 * 10000;

    /**
     * 布隆过滤器误差率百分比 (0, 1.0)
     * 误差率。该值越小，占用内存越大
     */
    private double fpp = 0.05D;

    /**
     * 布隆过滤器
     */
    private BloomFilter<T> bloomFilter;

    public BloomFilterUtil() {
        this(1000 * 10000, 0.05D);
    }

    public BloomFilterUtil(int expectedInsertions, double fpp) {
        this.expectedInsertions = expectedInsertions;
        this.fpp = fpp;
        this.bloomFilter = (BloomFilter<T>) BloomFilter.create(Funnels.integerFunnel(), expectedInsertions, fpp);
    }

    public final BloomFilter<T> getBloomFilter() {
        return this.bloomFilter;
    }

}
